function reservedDecimal(num, fixed = 1) {
	if (!num) return Number(0).toFixed(fixed);
	num = String(num);
	let splitNum = num.split('.')
	let firstNum = splitNum[0];
	let lastNum = splitNum[1] ? String(splitNum[1]) : (new Array(fixed + 1)).join('0');
	let toFixedNum = (lastNum && lastNum.length) || 0;
	toFixedNum = toFixedNum > fixed ? fixed : toFixedNum;
	//toFixed会进行四舍五入 所以我们用裁剪
	// return .toFixed(toFixedNum);
	return firstNum + (toFixedNum > 0 ? '.' : '') + lastNum.substr(0, toFixedNum)
}

// 转换单位
function unitConvert(num) {
	if(!num && num !== 0) return num || 0;
    var moneyUnits = ["", "w"] 
    var dividend = 10000;
    var curentNum = num || 0;
    //转换数字 
    var curentUnit = moneyUnits[0];
    //转换单位 
    for (var i = 0; i <2; i++) { 
        curentUnit = moneyUnits[i] 
        if(strNumSize(curentNum)<5){ 
            break;
        }
        curentNum = curentNum / dividend 
    }
	return reservedDecimal(curentNum, 1) * 1 + curentUnit;
}

function strNumSize(tempNum){ 
    var stringNum = tempNum.toString() 
    var index = stringNum.indexOf(".") 
    var newNum = stringNum;
    if(index!=-1){
        newNum = stringNum.substring(0,index) 
    } 
    return newNum.length
}

export default {
	price: reservedDecimal,
    unit: unitConvert
}